package com.bckj.fastboot.core.redis;

import lombok.RequiredArgsConstructor;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.FileCopyUtils;

import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

@RequiredArgsConstructor
public class LuaScriptLoader {

	private final ResourceLoader resourceLoader;

	public String loadScript(String scriptPath) {
		Resource resource = resourceLoader.getResource(scriptPath);
		try (InputStreamReader reader = new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8)) {
			return FileCopyUtils.copyToString(reader);
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("加载lua脚本失败: " + scriptPath, e);
		}
	}
}